package com.tester.yaa.exam002.multiThread;

import java.util.Random;

/**
 * @Aduthor yinanan
 * @ClassName GuessThread
 * @Date 2022/12/11 16:57
 * @Description TODO
 **/
public class GuessThread extends Thread {

    //最大值和最小值
    private int min = 0, max = 100, nownum;

    //比较结果
    String Result;

    public void run() {
        int num = 0;
        while (true) {
            num++;
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Random ran = new Random();
            //当前猜的数字（最大值和最小值之间的数）
            nownum = min + ran.nextInt(max - min);
            //调用给出整数的线程 的猜数字方法integerNumber
            Result = IntegerThread.integerNumber(nownum);
            if (Result.equals("你猜小了")) {
                min = nownum;
                System.out.println("我第" + num + "次猜的数字是：" + nownum);
                System.out.println("你猜小了");
            } else if (Result.equals("你猜大了")) {
                max = nownum;
                System.out.println("我第" + num + "次猜的数字是：" + nownum);
                System.out.println("你猜大了");
            } else {
                System.out.println("我第" + num + "次猜的数字是：" + nownum);
                System.out.println("恭喜，你猜对了");
                System.exit(0);
            }
        }
    }
}
